﻿using System.Collections.Generic;
using IDAL;
using Model;
using NHibernate;

namespace SqlDALLibrary
{
    public class ResourceDal : IResourceDAL
    {
        private static EntityControl control;

        static ResourceDal()
        {
            control = EntityControl.CreateEntityControl("Model");
        }

        public Resource saveResouce(Resource resource)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Save(resource);
                tx.Commit();
            }
            session.Close();
            return resource;
        }

        public void updateResouce(Resource resource)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Update(resource);
                tx.Commit();
            }
            session.Close();
        }

        public void deleteResource(Resource resource)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            using (var tx = session.BeginTransaction())
            {
                session.Delete(resource);
                tx.Commit();
            }
            session.Close();
        }

        public IList<Resource> getResouce(string hql)
        {
            return control.getSession()
                .CreateQuery(hql)
                .List<Resource>();
        }

        public Resource getResouceById(int id)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            Resource resource = session
                .CreateQuery("from Resource r where r.id=:id")
                .SetInt32("id", id)
                .UniqueResult<Resource>();
            session.Close();
            return resource;
        }

        public IList<Resource> getAllResource()
        {
            return control.getSession()
                .CreateQuery("from Resource")
                .List<Resource>();
        }

        public IList<Resource> getUserResouce(User user)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<Resource> resourceList = session
                .CreateQuery("from Resource r where r.uploader=:uploader")
                .SetParameter("uploader", user)
                .List<Resource>();
            session.Close();
            return resourceList;
        }

        public IList<Resource> getUserResouce(User user, int status)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<Resource> resourceList = session
                .CreateQuery("from Resource r where r.uploader=:uploader and r.status=:status")
                .SetParameter("uploader", user)
                .SetInt32("status", status)
                .List<Resource>();
            session.Close();
            return resourceList;
        }

        public IList<Resource> getResourceByState(int status)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<Resource> resourceList = session
                .CreateQuery("from Resource r where  r.status=:status")
                .SetInt32("status", status)
                .List<Resource>();
            session.Close();
            return resourceList;
        }

        public IList<Resource> getRourceByCategory(Category category)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<Resource> resourceList = session
                .CreateQuery("from Resource r where r.category=:category ORDER BY (r.marks/r.marksUserNum) desc")
                .SetParameter("category", category)
                .List<Resource>();
            session.Close();
            return resourceList;
        }

        public IList<Resource> getRourceByKeyword(string keyword)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            IList<Resource> resourceList = session
                .CreateQuery("from Resource r where r.keyword=:keyword")
                .SetParameter("keyword", keyword)
                .List<Resource>();
            session.Close();
            return resourceList;
        }

        public IList<Resource> searchResource(string queryStr)
        {
            NHibernateHelper nh = new NHibernateHelper();
            ISession session = nh.GetSession();
            string hql = "FROM Resource r WHERE r.resourceName LIKE :name or r.description LIKE :description";
            IList<Resource> resourceList = session
                .CreateQuery(hql)
                .SetParameter("name", "%" + queryStr + "%")
                .SetParameter("description", "%" + queryStr + "%")
                .List<Resource>();
            session.Close();
            return resourceList;
        }

    }
}
